package com.fengwei.config;

import com.fengwei.util.SnowflakeIdWorker;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * Snowflake算法
 */
@Configuration
public class SnowflakeConfig {
    private static final Logger logger = LoggerFactory.getLogger(SnowflakeConfig.class);

    /**
     * snowflake算法中的数据中心ID，一般设置为0，不用修改
     */
    @Value("${snowflake.datacenter.id}")
    private long snowflakeDatacenterId;

    /**
     * snowflake算法中的机器ID，一般从0-31，按照部署机器
     */
    @Value("${snowflake.worker.id}")
    private long snowflakeWorkerId;

    /**
     * 唯一ID的bean
     */
    @Bean(name = "snowflakeIdWorker")
    public SnowflakeIdWorker snowflakeIdWorker() {
        SnowflakeIdWorker snowflakeIdWorker = new SnowflakeIdWorker(snowflakeWorkerId,
                snowflakeDatacenterId);
        logger.info("{} SnowflakeIdWorker has been created!", "snowflakeIdWorker");
        return snowflakeIdWorker;
    }

}
